package org.maven.ide.eclipse.wtp.ext;

import java.util.List;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jst.j2ee.web.project.facet.WebFacetUtils;
import org.eclipse.wst.common.componentcore.ComponentCore;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
import org.eclipse.wst.common.componentcore.resources.IVirtualReference;
import org.eclipse.wst.common.project.facet.core.IFacetedProject;
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
import org.maven.ide.eclipse.project.ResolverConfiguration;
import org.maven.ide.eclipse.tests.AsbtractMavenProjectTestCase;

/**
 * WTPProjectConfiguratorTest
 *
 * @author jer@printstacktrace.org (MNGECLIPSE-599)
 */
public class WTPProjectConfiguratorTest extends AsbtractMavenProjectTestCase {

  public void testMNGECLIPSE599_waroverlay() throws Exception {
    waitForJobsToComplete();
    IProject[] projects = importProjects("projects/MNGECLIPSE-599", 
        new String[] {"testTapestry/pom.xml", "testWarOverlay/pom.xml", }, 
        new ResolverConfiguration());

    waitForJobsToComplete();
    assertEquals(2, projects.length);
    IProject dep = projects[1];
    IProject main = projects[0];

    
    dep.build(0, new NullProgressMonitor());
    waitForJobsToComplete();
    
    List<IMarker> markers = findErrorMarkers(main);
    System.out.println(toString(markers.toArray(new IMarker[markers.size()])));
    
    assertMarkers(main, 0);
    assertMarkers(dep, 0);

    IFacetedProject mainWar = ProjectFacetsManager.create(main);
    assertNotNull(mainWar);
    assertTrue(mainWar.hasProjectFacet(WebFacetUtils.WEB_FACET));

    IFacetedProject depWar = ProjectFacetsManager.create(dep);
    assertNotNull(depWar);
    assertTrue(depWar.hasProjectFacet(WebFacetUtils.WEB_FACET));

    IVirtualComponent comp = ComponentCore.createComponent(main);
    IVirtualReference[] references = comp.getReferences();
    IVirtualReference depRef = references[0];
    assertEquals(dep, depRef.getReferencedComponent().getProject());
    
    assertTrue(comp.getRootFolder().getFile("warOverlay.html").exists());
    assertTrue(comp.getRootFolder().getFile("warOverlayInArchive.html").exists());
    
  }



 
}
